"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoomManager = void 0;
const UserDao_1 = require("../../account_server/dao/UserDao");
const RoomDao_1 = require("../../hall_server/dao/RoomDao");
const RoomModel_1 = require("../../hall_server/model/RoomModel");
const LogServerHelper_1 = require("../../log_server/LogServerHelper");
const Config_1 = require("../config/Config");
const ErrorCode_1 = require("../config/ErrorCode");
const GameConfig_1 = require("../config/GameConfig");
const RoomConfig_1 = require("../config/RoomConfig");
const SocketConnectionManager_1 = require("../engine/socketserver/socket/SocketConnectionManager");
const Logger_1 = require("../engine/utils/Logger");
const ResponseModel_1 = require("../model/ResponseModel");
const SystemSettingModel_1 = require("../model/SystemSettingModel");
const ServerManager_1 = require("./ServerManager");
class RoomManager {
    delete_room(deleteRoomIdList, deleteRoomList = [], msg, saveVideo = true) {
        return __awaiter(this, void 0, void 0, function* () {
            if (deleteRoomIdList.length <= 0) {
                return false;
            }
            // let dbSuc: boolean = await RoomDao.delete_room(deleteRoomIdList)
            RoomDao_1.RoomDao.delete_room(deleteRoomIdList);
            let dbSuc = true;
            if (dbSuc) {
                for (let i = 0; i < deleteRoomList.length; i++) {
                    let deleteRoom = deleteRoomList[i];
                    if (deleteRoom.numOfTurns <= 1) {
                        // RoomManager.instance.checkAddRoomCost(deleteRoom)
                    }
                    if (deleteRoom.numOfTurns > 0) {
                        if (deleteRoom.roomType == RoomConfig_1.RoomConfig.Room_Type.Create || deleteRoom.roomType == RoomConfig_1.RoomConfig.Room_Type.Ziyou) {
                            // await GameManager.instance.notify_game_result(deleteRoom)
                            // if (saveVideo) {
                            //     if (deleteRoom.gameType == GameConfig.Game_Type.huanghua_mj || deleteRoom.gameType == GameConfig.Game_Type.ddz
                            //         || deleteRoom.gameType == GameConfig.Game_Type.cangxian_mj || deleteRoom.gameType == GameConfig.Game_Type.dazhonger_mj
                            //         || deleteRoom.gameType == GameConfig.Game_Type.jianziding || deleteRoom.gameType == GameConfig.Game_Type.paodekuai) {
                            //         let video: VideoInfo = deleteRoom.gameResult.videoList[deleteRoom.num_of_turns - 1] as VideoInfo;
                            //         if (video) {
                            //             video.time = DateUtil.now()
                            //             video.ju = deleteRoom.num_of_turns
                            //             if (deleteRoom.gameType == GameConfig.Game_Type.huanghua_mj) {
                            //                 let gameResult: huanghua_mj_GameResult = new huanghua_mj_GameResult();
                            //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                            //                     gameResult.scoreList[j] = 0
                            //                 }
                            //                 video.videoResult = gameResult
                            //             }
                            //             else if (deleteRoom.gameType == GameConfig.Game_Type.ddz) {
                            //                 let gameResult: DdzGameResult = new DdzGameResult();
                            //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                            //                     gameResult.scoreList[j] = 0
                            //                 }
                            //                 video.videoResult = gameResult
                            //             }
                            //             else if (deleteRoom.gameType == GameConfig.Game_Type.jianziding) {
                            //                 let gameResult: jianziding_GameResult = new jianziding_GameResult();
                            //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                            //                     gameResult.scoreList[j] = 0
                            //                 }
                            //                 video.videoResult = gameResult
                            //             }
                            //             else if (deleteRoom.gameType == GameConfig.Game_Type.paodekuai) {
                            //                 let gameResult: paodekuai_GameResult = new paodekuai_GameResult();
                            //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                            //                     gameResult.scoreList[j] = 0
                            //                 }
                            //                 video.videoResult = gameResult
                            //             }
                            //             else if (deleteRoom.gameType == GameConfig.Game_Type.dazhonger_mj) {
                            //                 let gameResult: dazhonger_mj_GameResult = new dazhonger_mj_GameResult();
                            //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                            //                     gameResult.scoreList[j] = 0
                            //                 }
                            //                 video.videoResult = gameResult
                            //             }
                            //             else if (deleteRoom.gameType == GameConfig.Game_Type.cangxian_mj) {
                            //                 let gameResult: cangxian_mj_GameResult = new cangxian_mj_GameResult();
                            //                 for (let j = 0; j < deleteRoom.playerList.length; j++) {
                            //                     gameResult.scoreList[j] = 0
                            //                 }
                            //                 video.videoResult = gameResult
                            //             }
                            //             await GameManager.instance.add_game_result(deleteRoom)
                            //             video.gameResultId = deleteRoom.gameResult.id;
                            //         }
                            //         // Logger.log("删除房间==========", deleteRoom.gameResult.id, video.gameResultId)
                            //         if (deleteRoom.roomType == RoomConfig.Room_Type.Create) {
                            //             await RoomVideoDao.insertVideo(video)
                            //         }
                            //     }
                            // }
                        }
                    }
                    // RoomManager.instance.notifyRoomRelease(deleteRoom, msg[i])
                }
            }
            return dbSuc;
        });
    }
    leave_room(room_id, userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let room = RoomModel_1.RoomModel.roomMap.get(room_id);
            let respModel = new ResponseModel_1.ResponseModel();
            if (room) {
                let canLeave = true;
                if (room.numOfTurns > 0) {
                    canLeave = false;
                    respModel.code = ErrorCode_1.ErrorCode.Code.Fail;
                    respModel.msg = "游戏已经开始不能离开";
                }
                else {
                    if (userid == room.creator) {
                        if (room.roomConfigBase.costType == RoomConfig_1.RoomConfig.cost_type.fangzhu) { //房主支付的房间要所有人都跑了才能走
                            // if (room.getPlayerNum() > 1) {
                            //     canLeave = false;
                            //     respModel.code = ErrorCode.Code.Fail
                            //     respModel.msg = "房主支付的房间要其他人都走了才能解散";
                            // }
                        }
                    }
                }
                if (canLeave) {
                    respModel.code = ErrorCode_1.ErrorCode.Code.Suc;
                    // room.leavePlayer(userid)
                    // await RoomDao.update_room(room)
                    // await UserDao.setRoom_id(userid, 0)
                }
            }
            else {
                respModel.code = ErrorCode_1.ErrorCode.Code.Fail;
                respModel.msg = "不在房间里面";
            }
            if (respModel.code == ErrorCode_1.ErrorCode.Code.Suc) {
                // ServerManager.instance.leaveRoom(room)
                // RoomManager.instance.notify_player_leave(room, userid)
            }
            return respModel;
        });
    }
    notifyRoomRelease(room, msg = "时间到，该房间由系统释放") {
        ServerManager_1.ServerManager.instance.deleteRoom(room.room_id);
        for (let i = 0; i < room.playerList.length; i++) {
            let player = room.playerList[i];
            if (player) {
                let conn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid);
                if (conn) {
                    let respModel = new ResponseModel_1.ResponseModel();
                    respModel.code = ErrorCode_1.ErrorCode.Code.Suc;
                    respModel.msg = msg;
                    // conn.createMsgToClient(0, ProtoConfig.notify_room_release, respModel)
                }
            }
        }
    }
    notify_player_update(room) {
        if (Config_1.Config.serverType == Config_1.Config.ServerType_List.hall_server) {
            return;
        }
        for (let i = 0; i < room.playerList.length; i++) {
            let player = room.playerList[i];
            if (player) {
                let conn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid);
                if (conn) {
                    let respModel = new ResponseModel_1.ResponseModel();
                    respModel.code = ErrorCode_1.ErrorCode.Code.Suc;
                    respModel.data = room.playerList;
                    // conn.createMsgToClient(0, ProtoConfig.notify_player_update, respModel)
                }
            }
        }
    }
    notify_player_join(room) {
        if (Config_1.Config.serverType == Config_1.Config.ServerType_List.hall_server) {
            return;
        }
        for (let i = 0; i < room.playerList.length; i++) {
            let player = room.playerList[i];
            if (player) {
                let conn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid);
                if (conn) {
                    let respModel = new ResponseModel_1.ResponseModel();
                    respModel.code = ErrorCode_1.ErrorCode.Code.Suc;
                    respModel.data = room.playerList;
                    // conn.createMsgToClient(0, ProtoConfig.notify_player_join, respModel)
                }
            }
        }
    }
    notify_player_leave(room, leave_userid) {
        return __awaiter(this, void 0, void 0, function* () {
            if (Config_1.Config.serverType == Config_1.Config.ServerType_List.hall_server) {
                return;
            }
            let respModel = new ResponseModel_1.ResponseModel();
            respModel.code = ErrorCode_1.ErrorCode.Code.Suc;
            let respData = {
                "leave_userid": leave_userid,
                "playerList": room.playerList
            };
            respModel.data = respData;
            let conn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUserid(leave_userid);
            if (conn) {
                // conn.createMsgToClient(0, ProtoConfig.notify_player_leave, respModel)
            }
            for (let i = 0; i < room.playerList.length; i++) {
                let player = room.playerList[i];
                if (player) {
                    let conn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid);
                    if (conn) {
                        // conn.createMsgToClient(0, ProtoConfig.notify_player_leave, respModel)
                    }
                }
            }
            // if (room.getPlayerNum() == 0 && room.roomType != RoomConfig.Room_Type.Ziyou) {
            if (RoomModel_1.RoomModel.getPlayerNum(room) == 0) {
                Logger_1.Logger.log("notify_player_leave===所有人都离开房间了回收房间=", room);
                let dbSuc = yield RoomManager.instance.delete_room([room.room_id], [room], ["所有人都离开房间了回收房间"]);
                if (dbSuc) {
                }
                else {
                    respModel.code = ErrorCode_1.ErrorCode.Code.Fail;
                    respModel.msg = "清除房间信息失败";
                }
            }
        });
    }
    notify_game_start(room) {
        return __awaiter(this, void 0, void 0, function* () {
            if (Config_1.Config.serverType == Config_1.Config.ServerType_List.hall_server) {
                return;
            }
            room.lastCheckAutoReadyTIme = 0;
            // if (Config.serverType == Config.ServerType_List.ddz_server) {
            //     ddz_Gamemanager.instance.notify_game_start(room, true)
            // }
            // else if (Config.serverType == Config.ServerType_List.paodekuai_server) {
            //     paodekuai_Gamemanager.instance.notify_game_start(room, true)
            // }
            // else if (Config.serverType == Config.ServerType_List.jianziding_server) {
            //     jianziding_Gamemanager.instance.notify_game_start(room, true)
            // }
            // else if (Config.serverType == Config.ServerType_List.huanghua_mj_server) {
            //     huanghua_mj_Gamemanager.instance.notify_game_start(room, true)
            // }
            // else if (Config.serverType == Config.ServerType_List.cangxian_mj_server) {
            //     cangxian_mj_Gamemanager.instance.notify_game_start(room, true)
            // }
            // else if (Config.serverType == Config.ServerType_List.dazhonger_mj_server) {
            //     dazhonger_mj_Gamemanager.instance.notify_game_start(room, true)
            // }
            //通知其他服务器增加回合
            ServerManager_1.ServerManager.instance.notify_game_start(room);
        });
    }
    checkDecRoomCost(room) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.decRoomCreatorCost(room);
            yield this.decRoompingjunCost(room);
            yield this.decRoomJinbi(room);
        });
    }
    decRoomMatchJinbi(room) {
        return __awaiter(this, void 0, void 0, function* () {
            if (room.roomType == RoomConfig_1.RoomConfig.Room_Type.Match) {
                if (room.conf["cost_type"] == RoomConfig_1.RoomConfig.cost_type.enterMinJinbi) {
                    let decNum = room.conf["cost"];
                    for (let i = 0; i < room.playerList.length; i++) {
                        let player = room.playerList[i];
                        if (player && player.matchTurn == 1) {
                            let conn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid);
                            // if (conn && conn.isLogin() && conn.user) {
                            //     conn.user.coins -= decNum
                            //     conn.user.coins = await UserDao.decCoins(player.userid, decNum);
                            //     LogServerHelper.log_add_user_coins_history(conn.user.userid, -decNum, conn.user.coins, GameConfig.Coins_History_Reason.match_create, "比赛场服务费，扣除了" + decNum + "金豆")
                            // }
                        }
                    }
                }
            }
        });
    }
    //房主支付 扣钱
    decRoomCreatorCost(room) {
        return __awaiter(this, void 0, void 0, function* () {
            if (room.conf["cost_type"] == RoomConfig_1.RoomConfig.cost_type.fangzhu && room.num_of_turns == 1) {
                let player = room.getPlayerByUserid(room.creator);
                if (player) {
                    let conn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid);
                    if (conn && conn.isLogin() && conn.user) {
                        // conn.user.money -= room.conf["cost"]
                        // conn.user.money = await UserDao.decGems(player.userid, room.conf["cost"]);
                        // LogServerHelper.log_add_user_spend_daily(player.userid, "gems", room.conf["cost"])
                        // LogServerHelper.log_add_user_gems_history(player.userid, -room.conf["cost"], conn.user.money, GameConfig.Gems_History_Reason.Create_Room, GameConfig.GameNameList[room.gameType] + "房主支付，创建房间扣除钻石" + room.conf["cost"])
                    }
                }
                else {
                    Logger_1.Logger.warn("房主支付发现不存在房主:", room);
                }
            }
        });
    }
    checkAddRoomCost(room) {
        // this.addRoomCreatorCost(room)
        // this.addRoompingjunCost(room)
        this.addRoomClubOwnerCost(room);
    }
    //房主支付 退钱
    addRoomCreatorCost(room) {
        return __awaiter(this, void 0, void 0, function* () {
            if (room.conf["cost_type"] == RoomConfig_1.RoomConfig.cost_type.fangzhu && room.num_of_turns == 1) {
                let player = room.getPlayerByUserid(room.creator);
                if (player) {
                    let conn = SocketConnectionManager_1.SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid);
                    // if (conn && conn.isLogin() && conn.user) {
                    //     conn.user.money += room.conf["cost"]
                    //     conn.user.money = await UserDao.addGems(player.userid, room.conf["cost"]);
                    //     LogServerHelper.log_add_user_gems_history(player.userid, room.conf["cost"], conn.user.money, GameConfig.Gems_History_Reason.Dismiss_Room, GameConfig.GameNameList[room.gameType] + "房主支付，解散房间退回钻石" + room.conf["cost"])
                    // }
                }
                else {
                    Logger_1.Logger.warn("房主支付发现不存在房主:", room);
                }
            }
        });
    }
    //平摊房费
    decRoompingjunCost(room) {
        return __awaiter(this, void 0, void 0, function* () {
            if (room.conf["cost_type"] == RoomConfig_1.RoomConfig.cost_type.pingjun && room.num_of_turns == 1) {
                let decNum = Math.floor(room.conf["cost"] / room.conf["renshu"]);
                for (let i = 0; i < room.playerList.length; i++) {
                    let player = room.playerList[i];
                    if (player) {
                        // let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                        // if (conn && conn.isLogin() && conn.user) {
                        //     let user: UserModel = conn.user;
                        //     user.money -= decNum
                        //     user.money = await UserDao.decGems(player.userid, decNum);
                        //     LogServerHelper.log_add_user_gems_history(player.userid, -decNum, user.money, GameConfig.Gems_History_Reason.Create_Room, GameConfig.GameNameList[room.gameType] + "平摊房费，创建房间扣除钻石" + decNum)
                        //     LogServerHelper.log_add_user_spend_daily(player.userid, "gems", decNum)
                        // }
                    }
                }
            }
        });
    }
    //平摊房费 退钱
    addRoompingjunCost(room) {
        return __awaiter(this, void 0, void 0, function* () {
            if (room.conf["cost_type"] == RoomConfig_1.RoomConfig.cost_type.pingjun && room.num_of_turns == 1) {
                let decNum = Math.floor(room.conf["cost"] / room.conf["renshu"]);
                for (let i = 0; i < room.playerList.length; i++) {
                    // let player: RoomPlayer = room.playerList[i];
                    // if (player) {
                    //     let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                    //     if (conn && conn.isLogin() && conn.user) {
                    //         conn.user.money += decNum
                    //         conn.user.money = await UserDao.addGems(player.userid, decNum);
                    //         LogServerHelper.log_add_user_gems_history(player.userid, decNum, conn.user.money, GameConfig.Gems_History_Reason.Dismiss_Room, GameConfig.GameNameList[room.gameType] + "平摊房费，解散房间退回钻石" + decNum)
                    //     }
                    // }
                }
            }
        });
    }
    //大赢家付款
    decRoomDayingjiaCost(room, bigWinUserid) {
        return __awaiter(this, void 0, void 0, function* () {
            if (room.conf["cost_type"] == RoomConfig_1.RoomConfig.cost_type.dayingjia && room.num_of_turns == 1) {
                let player = room.getPlayerByUserid(bigWinUserid);
                if (player) {
                    // let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                    // if (conn && conn.isLogin() && conn.user) {
                    //     conn.user.money -= room.conf["cost"]
                    //     conn.user.money = await UserDao.decGems(player.userid, room.conf["cost"]);
                    //     LogServerHelper.log_add_user_gems_history(player.userid, -room.conf["cost"], conn.user.money, GameConfig.Gems_History_Reason.Create_Room, GameConfig.GameNameList[room.gameType] + "大赢家付款，创建房间扣除钻石" + room.conf["cost"])
                    //     LogServerHelper.log_add_user_spend_daily(player.userid, "gems", room.conf["cost"])
                    // }
                }
                else {
                    Logger_1.Logger.warn("大赢家支付发现不存在大赢家:", room);
                }
            }
        });
    }
    //圈主支付
    decRoomClubOwnerCost(userid, cost, gameType) {
        return __awaiter(this, void 0, void 0, function* () {
            let left_gems = yield UserDao_1.UserDao.decGems(userid, cost);
            ServerManager_1.ServerManager.instance.notify_update_gems(userid);
            LogServerHelper_1.LogServerHelper.log_add_user_gems_history(userid, -cost, left_gems, GameConfig_1.GameConfig.Gems_History_Reason.Create_Room, GameConfig_1.GameConfig.GameNameList[gameType] + "圈主支付，创建房间扣除钻石" + cost);
            LogServerHelper_1.LogServerHelper.log_add_user_spend_daily(userid, "gems", cost);
            return true;
        });
    }
    //圈主支付退钱
    addRoomClubOwnerCost(room) {
        return __awaiter(this, void 0, void 0, function* () {
            if (room.conf["cost_type"] == RoomConfig_1.RoomConfig.cost_type.quanzhu && room.num_of_turns <= 1 && room.clubid > 0) {
                // let clubObj: object = await ClubDao.getClub(room.clubid);
                // if (clubObj) {
                // let creator: number = clubObj["creator"]
                // let clubOwnerConn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(creator)
                // if (clubOwnerConn && clubOwnerConn.isLogin() && clubOwnerConn.user) {
                //     clubOwnerConn.user.money += room.conf["cost"]
                // }
                // let left_gems: number = await UserDao.addGems(creator, room.conf["cost"]);
                // ServerManager.instance.notify_update_gems(creator)
                // LogServerHelper.log_add_user_gems_history(creator, room.conf["cost"], left_gems, GameConfig.Gems_History_Reason.Dismiss_Room, GameConfig.GameNameList[room.gameType] + "圈主支付，解散房间退回钻石" + room.conf["cost"])
                // }
            }
        });
    }
    decRoomJinbi(room) {
        return __awaiter(this, void 0, void 0, function* () {
            if (room.roomType == RoomConfig_1.RoomConfig.Room_Type.Jinbi) {
                if (room.conf["cost_type"] == RoomConfig_1.RoomConfig.cost_type.enterMinJinbi) {
                    let decNum = room.conf["cost"];
                    for (let i = 0; i < room.playerList.length; i++) {
                        let player = room.playerList[i];
                        if (player) {
                            // let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                            // if (conn && conn.isLogin() && conn.user) {
                            //     conn.user.coins -= decNum
                            //     conn.user.coins = await UserDao.decCoins(player.userid, decNum);
                            //     LogServerHelper.log_add_user_coins_history(conn.user.userid, -decNum, conn.user.coins, GameConfig.Coins_History_Reason.JinbiChang_create, "金豆场金豆房，扣除了" + decNum + "金豆")
                            //     player.score = conn.user.coins
                            // }
                        }
                    }
                }
            }
            else if (room.roomType == RoomConfig_1.RoomConfig.Room_Type.Jinbi_Special) {
                if (room.conf["cost_type"] == RoomConfig_1.RoomConfig.cost_type.enterMinJinbi) {
                    let decNum = room.conf["cost"];
                    for (let i = 0; i < room.playerList.length; i++) {
                        let player = room.playerList[i];
                        if (player) {
                            // let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                            // if (conn && conn.isLogin() && conn.user) {
                            //     conn.user.coins -= decNum
                            //     conn.user.coins = await UserDao.decCoins(player.userid, decNum);
                            //     LogServerHelper.log_add_user_coins_history(conn.user.userid, -decNum, conn.user.coins, GameConfig.Coins_History_Reason.Jinbi_Special_create, "金豆场特殊房，扣除了" + decNum + "金豆")
                            //     player.score = conn.user.coins
                            // }
                        }
                    }
                }
            }
            else if (room.roomType == RoomConfig_1.RoomConfig.Room_Type.Ziyou) {
                if (room.conf["cost_type"] == RoomConfig_1.RoomConfig.cost_type.enterMinJinbi && room.num_of_turns == 1) {
                    let decNum = room.conf["cost"];
                    for (let i = 0; i < room.playerList.length; i++) {
                        let player = room.playerList[i];
                        if (player) {
                            // let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                            // if (conn && conn.isLogin() && conn.user) {
                            //     conn.user.coins -= decNum
                            //     conn.user.coins = await UserDao.decCoins(player.userid, decNum);
                            //     LogServerHelper.log_add_user_coins_history(conn.user.userid, -decNum, conn.user.coins, GameConfig.Coins_History_Reason.Ziyouchang_create, "自由场房，扣除了" + decNum + "金豆")
                            //     player.score = conn.user.coins
                            // }
                        }
                    }
                }
            }
            else if (room.roomType == RoomConfig_1.RoomConfig.Room_Type.Match) {
                // if (room.conf["cost_type"] == RoomConfig.cost_type.enterMinJinbi) {
                //     let decNum: number = room.conf["cost"]
                //     for (let i = 0; i < room.playerList.length; i++) {
                //         let player: RoomPlayer = room.playerList[i];
                //         if (player) {
                //             let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                //             if (conn && conn.isLogin() && conn.user) {
                //                 conn.user.coins -= decNum
                //                 conn.user.coins = await UserDao.decCoins(player.userid, decNum);
                //                 LogServerHelper.log_add_user_coins_history(conn.user.userid, -decNum, conn.user.coins, GameConfig.Coins_History_Reason.match_create, "比赛场服务费，扣除了" + decNum + "金豆")
                //             }
                //         }
                //     }
                // }
            }
        });
    }
    checkUpdatePlayerCoin(user) {
        return __awaiter(this, void 0, void 0, function* () {
            // let room_id: number = user.room_id
            // let room: RoomModel = RoomModel.roomMap.get(room_id)
            // if (RoomModel.roomMap.has(room_id)) {
            //     if (room.roomType == RoomConfig.Room_Type.Ziyou) {
            //         for (let i = 0; i < room.playerList.length; i++) {
            //             if (room.playerList[i] && room.playerList[i].userid == user.userid) {
            //                 room.playerList[i].score = user.coins;
            //                 RoomManager.instance.notify_player_update(room)
            //                 break;
            //             }
            //         }
            //     }
            // }
        });
    }
    sendDailiCoins(userid, coins, dailiLevel = 1) {
        return __awaiter(this, void 0, void 0, function* () {
            // let percentArr: Array<number> = [60, 10, 5, 5]
            if (dailiLevel < 3) { //最多有三级代理
                let choushui = SystemSettingModel_1.SystemSettingModel.instance.ziyouchang_ticheng;
                let percentArr = choushui.split(",");
                let sendPercent = parseInt(percentArr[dailiLevel - 1]);
                let userData = yield UserDao_1.UserDao.get_user_data_by_userid(userid);
                let income = userData["income"];
                let qudaodaili = userData["qudaodaili"];
                if (qudaodaili == 1) {
                    sendPercent += parseInt(percentArr[3]);
                }
                sendPercent = sendPercent / 100;
                let nowIncome = income + coins * sendPercent;
                yield UserDao_1.UserDao.update_income(userid, nowIncome);
                let invite = userData["invite"];
                if (invite && invite > 0) {
                    dailiLevel++;
                    this.sendDailiCoins(invite, coins, dailiLevel);
                }
            }
        });
    }
}
exports.RoomManager = RoomManager;
RoomManager.instance = new RoomManager();
